﻿using System;
using System.Windows;
using System.Windows.Controls;
using Gao.Poker.App.Print;
using Gao.Poker.App.ViewModel;
using Gao.Poker.Domain;
using Gao.Poker.Services;

namespace Gao.Poker.App.Store
{
    /// <summary>
    /// Interaction logic for MarketView.xaml
    /// </summary>
    public partial class MarketView : UserControl
    {
       public ConsumptionViewModel ViewModel { get; set; }

       public MarketView()
        {
            InitializeComponent();
            ViewModel = new ConsumptionViewModel();
            ViewModel.Message = OnMessage;
            DataContext = ViewModel;
        }

        private void OnMessage(string obj)
        {
            MessageBox.Show(obj, "提示消息");
        }

        private void MemberSearch_Click(object sender, RoutedEventArgs e)
        {
            ViewModel.OnPropertyChanged("Member");
        }

        private void ProductSearch_Click(object sender, RoutedEventArgs e)
        {

            //ViewModel.OnPropertyChanged("Product");
           ViewModel.GetProduct();
        }

        private void Submit_Click(object sender, RoutedEventArgs e)
        {
            if(ViewModel.Member == null )
            {
                OnMessage("请输入会员号");
                return;
            }

            if(ViewModel.ConsumptionProducts.Count == 0)
            {
                OnMessage("请选择销售商品");
                return;
            }

            if (ViewModel.Monetary <= 0)
            {
                OnMessage("销售金额总计不能为零");
                return;
            }
           
            var member = ViewModel.Member;

            if (member.Points < ViewModel.Monetary)
            {
                OnMessage("积分不足，请先充值！");
                return;
            }

            Consumption con = new Consumption();

            con.MemberCardNo = member.CardNo;
            con.MemberName = member.Name;
            con.Monetary = ViewModel.Monetary;
            con.CreateDt = DateTime.Now;

            foreach (var ph in ViewModel.ConsumptionProducts)
            {
                var cp = new ConsumptionProduct();
                cp.BarCode = ph.BarCode;
                cp.Discount = ph.Discount;
                cp.Number = ph.Number;
                cp.Price = ph.PIntegral;
                cp.ProductName = ph.ProductName;
                con.ConsumptionProducts.Add(cp);
            }

            ConsumptionService.Instance().Save(con);
            member.Points -= Convert.ToInt32(ViewModel.Monetary);

            MemberService.Instance().Save(member);

            //var print = new StorePrint();
            PrintHelper.Instance().StorePrint(ViewModel.Member, ViewModel.ConsumptionProducts, ViewModel.Monetary);

            var log = new MemberRecord();
            log.Type = 1;
            log.TypeName = "购买商品";
            log.MemberID = member.ID;
            log.Points = Convert.ToInt32(ViewModel.Monetary);
            MemberRecordService.Instance().Save(log);

            //PrintDialog dialog = new PrintDialog();
            //if (dialog.ShowDialog() == true)
            //{
            //    dialog.PrintVisual(print, "Store Print");
            //}

            OnMessage("销售成功");
        }
    }
}
